package com.qf.service.impl;

import com.qf.entity.Answers;
import com.qf.entity.Questions;
import com.qf.mapper.QuestionsMapper;
import com.qf.service.QuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/4
 */
@Service
public class QuestionsServiceImpl implements QuestionsService {

    @Autowired
    private QuestionsMapper questionsMapper;

    @Override
    public List<Questions> findAll() {
        //要求降序，逆向工程不适用
        return questionsMapper.findAllAndDescModifyTime();
    }

    @Override
    public List<Questions> findCountEqZero() {
        return questionsMapper.findCountEqZero();
    }

    @Override
    @Transactional
    public void insertQuestions(Questions questions) {
        questionsMapper.insertSelective(questions);
    }

    @Override
    public Questions findByQid(Integer qid) {
        return questionsMapper.findByQid(qid);
    }

    @Override
    @Transactional
    public void updateCountAddOneByQid(Integer qid) {

        int i = questionsMapper.updateCountAddOneByQid(qid);
        if (i!=1){
            System.out.println("更改回答问题次数失败");
            throw new RuntimeException("更改回答问题次数失败");
        }
    }

    @Override
    @Transactional
    public void updateLastModifedTime(Answers answers) {
        //将问题的最后修改时间，改为回答对象的答题时间
        int i = questionsMapper.updateLastModifedTime(answers);
        if (i!=1){
            System.out.println("修改时间失败");
            throw new RuntimeException("修改时间失败");
        }
    }
}
